
//import { hasExpectedRequestMetadata } from "@reduxjs/toolkit/dist/matchers"
import { Button, Modal } from 'antd';
import axios from "axios"
import Loading from "../components/Loading"
import storage from "./storage"
//组件外使用router编程式导航的方法
import { createBrowserHistory } from "history"

let history = createBrowserHistory()

let http = axios.create({
    baseURL: '/api',
    timeout: 5000,
    withCredentials: true
})

//请求拦截
http.interceptors.request.use(config => {
    let token = storage.get('wf-token')
    if (token) {
        config.headers['authorization'] = token
    }
    return config
})

//响应拦截
http.interceptors.response.use(config => {
    //判断后台返回的状态
    let { state, code } = config.data
    if (!state) {
        Modal.error({
            title: "错误提示",
            content: config.data.msg,
            centered: true
        })
    }
    //关闭加载动画
    Loading({ visible: false })

    //如果状态码是10022或1004，说明登录过期，要跳转到登录页
    if (code === '10022' || code === '1004') {
        history.push('/login')
    }

    return config
})

export default http